package leetcode35;


/**
 * @Author tyy
 * @Description 35. 搜索插入位置
 * 给定一个排序数组和一个目标值，在数组中找到目标值，并返回其索引。如果目标值不存在于数组中，返回它将会被按顺序插入的位置。
    请必须使用时间复杂度为 O(log n) 的算法。
 * @Since 2021/10/10
 */
public class Solution {

    public int searchInsert1(int[] nums, int target) {
        if (nums.length == 1) {
            return nums[0] < target? 1: 0;
        }
        for (int i = 0; i < nums.length - 1; i++) {
            int num = nums[i];
            // 最大和最小元素
            if (nums[0] > target) {
                return 0;
            }
            if (nums[nums.length-1] < target) {
                return nums.length;
            }
            if (num == target) {
                return i;
            }
            if (target > num && target <= nums[i+1]) {
                return ++i;
            }
        }
        return nums.length;
    }
    public int searchInsert2(int[] nums, int target) {
        for(int i = 0; i < nums.length;i++){
            if(nums[i] >= target){
                return i;
            }
        }
        return nums.length;
    }

    public int searchInsert3(int[] nums, int target) {

        int low = 0,high = nums.length - 1;
        int mid = 0;
        while (low <= high) {
            mid = (low + high) / 2;
            if (nums[mid] > target) {
                high = mid - 1;
            } else if (nums[mid] < target) {
                low = mid + 1;
            } else {
                return mid;
            }
        }
        if (low > high) {
            return low;
        } else {
            return high;
        }
    }
    public int searchInsert4_3(int[] nums, int target) {
        int left=0,right=nums.length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(nums[mid]<target){
                left=mid+1;
            }else if(nums[mid]>target){
                right=mid-1;
            }else{
                return mid;
            }
        }
        return left;
    }
    public int searchInsert5(int[] nums, int target) {
        int n = nums.length;

        // 定义target在左闭右闭的区间，[low, high]
        int low = 0;
        int high = n - 1;

        while (low <= high) { // 当low==high，区间[low, high]依然有效
            int mid = low + (high - low) / 2; // 防止溢出
            if (nums[mid] > target) {
                high = mid - 1; // target 在左区间，所以[low, mid - 1]
            } else if (nums[mid] < target) {
                low = mid + 1; // target 在右区间，所以[mid + 1, high]
            } else {
                // 1. 目标值等于数组中某一个元素  return mid;
                return mid;
            }
        }
        // 2.目标值在数组所有元素之前 3.目标值插入数组中 4.目标值在数组所有元素之后 return right + 1;
        return high + 1;
    }
    public int searchInsert(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while(left <= right) {
            int mid = (left + right) / 2;
            if(nums[mid] == target) {
                return mid;
            } else if(nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        System.out.println("left = " + left +"\nright = " + right );
        return left;
    }

    public static void main(String[] args) {
        int[] nums = {1,3,5,6};
        int target = 7;
        int i = new Solution().searchInsert(nums,target);
        System.out.println("i = " + i);
    }
}
